Skip to content

Conversation

artagnon
Copy link
Contributor

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Sep 18, 2025

@llvm/pr-subscribers-vectorizers

@llvm/pr-subscribers-llvm-transforms

Author: Ramkumar Ramachandra (artagnon)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/159558.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h (+26)
  • (modified) llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp (+5-4)
diff --git a/llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h b/llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h
index 8f9ce7a74b58b..7da75473ba00e 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h
@@ -176,6 +176,32 @@ inline int_pred_ty<is_zero_int> m_ZeroInt() {
 /// For vectors, this includes constants with undefined elements.
 inline int_pred_ty<is_one> m_One() { return int_pred_ty<is_one>(); }
 
+struct apint_match {
+  const APInt *&Res;
+
+  apint_match(const APInt *&Res) : Res(Res) {}
+
+  bool match(VPValue *VPV) const {
+    if (!VPV->isLiveIn())
+      return false;
+    Value *V = VPV->getLiveInIRValue();
+    if (!V)
+      return false;
+    const auto *CI = dyn_cast<ConstantInt>(V);
+    if (!CI && V->getType()->isVectorTy())
+      if (const auto *C = dyn_cast<Constant>(V))
+        CI = dyn_cast_or_null<ConstantInt>(
+            C->getSplatValue(/*AllowPoison=*/false));
+    if (!CI)
+      return false;
+    Res = &CI->getValue();
+    return true;
+  }
+};
+
+/// Match an APInt, capturing it if we match.
+inline apint_match m_APInt(const APInt *&C) { return C; }
+
 /// Matching combinators
 template <typename LTy, typename RTy> struct match_combine_or {
   LTy L;
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index 9996b0167edcb..c71655f08bf49 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -1592,11 +1592,12 @@ static bool tryToReplaceALMWithWideALM(VPlan &Plan, ElementCount VF,
           m_ActiveLaneMask(m_VPValue(Index), m_VPValue(), m_VPValue()));
     assert(Index && "Expected index from ActiveLaneMask instruction");
 
-    auto *II = dyn_cast<VPInstruction>(Index);
-    if (II && II->getOpcode() == VPInstruction::CanonicalIVIncrementForPart) {
-      auto Part = cast<ConstantInt>(II->getOperand(1)->getLiveInIRValue());
+    const APInt *Part;
+    if (match(Index,
+              m_VPInstruction<VPInstruction::CanonicalIVIncrementForPart>(
+                  m_VPValue(), m_APInt(Part))))
       Phis[Part->getZExtValue()] = Phi;
-    } else
+    else
       // Anything other than a CanonicalIVIncrementForPart is part 0
       Phis[0] = Phi;
   }

@artagnon artagnon changed the title [VPlanPatternMatch] Introduce m_APInt [VPlanPatternMatch] Introduce m_ConstantInt Sep 18, 2025
Copy link
Contributor

@lukel97 lukel97 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we will ever need to match and capture an APInt in VPlan but I guess this mirrors PatternMatch.h.

Copy link
Contributor

@fhahn fhahn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it would be simpler to first only have a m_ConstantInt instead of adding the extra indirection

Copy link
Contributor

@fhahn fhahn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

@artagnon artagnon merged commit b716d35 into llvm:main Sep 21, 2025
9 checks passed
@artagnon artagnon deleted the vplan-pm-apint branch September 21, 2025 12:27
ckoparkar added a commit to ckoparkar/llvm-project that referenced this pull request Sep 23, 2025
* main: (1562 commits)
  Document Policy on supporting newer C++ standard in LLVM codebase (llvm#156823)
  [MLIR][Transform][SMT] Introduce transform.smt.constrain_params (llvm#159450)
  Reapply "[compiler-rt] Remove %T from shared object substitutions (llvm#155302)"
  [NFC] [IndVarSimplify] Add non-overflowing usub test (llvm#159683)
  [Github] Remove separate tools checkout from pr-code workflows (llvm#159967)
  [clang] fix using enum redecl in template regression (llvm#159996)
  [DAG] Skip `mstore` combine for `<1 x ty>` vectors (llvm#159915)
  [mlir] Expose optional `PatternBenefit` to `func` populate functions (NFC) (llvm#159986)
  [LV] Set correct costs for interleave group members.
  [clang] ast-dump: use template pattern for `instantiated_from` (llvm#159952)
  [ARM] ha-alignstack-call.ll - regenerate test checks (llvm#159988)
  [LLD][MachO] Silence warning when building with MSVC
  [llvm][Analysis] Silence warning when building with MSVC
  [LV] Skip select cost for invariant divisors in legacy cost model.
  [Clang] Fix an error-recovery crash after d1a80de (llvm#159976)
  [VPlanPatternMatch] Introduce m_ConstantInt (llvm#159558)
  [GlobalISel] Add G_ABS computeKnownBits (llvm#154413)
  [gn build] Port 4cabd1e
  Reland "[clangd] Add feature modules registry" (llvm#154836)
  [LV] Also handle non-uniform scalarized loads when processing AddrDefs.
  ...
SeongjaeP pushed a commit to SeongjaeP/llvm-project that referenced this pull request Sep 23, 2025
YixingZhang007 pushed a commit to YixingZhang007/llvm-project that referenced this pull request Sep 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants